/******************************************************************************
 * Copyright (c) 2014 cocos2d-java.org
 * 
 *   http://www.cocos2d-java.org
 *   
 *   The MIT License (MIT)
 *      
 *Permission is hereby granted, free of charge, to any person obtaining a copy
 *of this software and associated documentation files (the "Software"), to deal
 *in the Software without restriction, including without limitation the rights
 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *copies of the Software, and to permit persons to whom the Software is
 *furnished to do so, subject to the following conditions:

 *The above copyright notice and this permission notice shall be included in
 *all copies or substantial portions of the Software.

 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *THE SOFTWARE.
 *******************************************************************************/

package cocos2d.actions.action;

import cocos2d.actions.action_intervals.CCActionInterval;
import cocos2d.base_nodes.CCNode;
import cocos2d.cocoa.ICCCopyable;

public class CCSpeed extends CCAction
{
    protected float m_fSpeed;
    protected CCActionInterval m_pInnerAction;

    public CCSpeed(CCActionInterval action, float fRate)
    {
        initWithAction(action, fRate);
    }

    protected CCSpeed(CCSpeed speed)
    {
    	super(speed);
        initWithAction((CCActionInterval) speed.m_pInnerAction.copy(), speed.m_fSpeed);
    }

    protected boolean initWithAction(CCActionInterval action, float fRate)
    {
        //Debug.Assert(action != null);

        m_pInnerAction = action;
        m_fSpeed = fRate;

        return true;
    }

    @Override
    public Object copy(ICCCopyable zone)
    {
        if (zone != null)
        {
        	CCSpeed ret = (CCSpeed) zone;
            super.copy(zone);

            ret.initWithAction((CCActionInterval) m_pInnerAction.copy(), m_fSpeed);

            return ret;
        }
        else
        {
            return new CCSpeed(this);
        }
    }

    public float getSpeed()
    {
        return m_fSpeed;
    }

    public void setSpeed(float value)
    {
        m_fSpeed = value;
    }
    
    @Override
    public void startWithTarget(CCNode target)
    {
        super.startWithTarget(target);
        m_pInnerAction.startWithTarget(target);
    }

    @Override
    public void stop()
    {
        m_pInnerAction.stop();
        super.stop();
    }

    @Override
    public void step(float dt) throws Exception
    {
        m_pInnerAction.step(dt * m_fSpeed);
    }

    @Override
    public boolean isDone()
    {
        return m_pInnerAction.isDone();
    }

    public CCActionInterval reverse()
    {
        return (CCActionInterval) (CCAction) new CCSpeed((CCActionInterval) m_pInnerAction.reverse(), m_fSpeed);
    }
}